//
//  DeviceVoiceListVC.swift
//  kxf
//
//  Created by LNS2 on 2024/4/21.
//

import Foundation

class DeviceVoiceListVC : WHBaseViewVC {
    
    var dataArray = NSArray()
    var choiceBlock:((NSDictionary)->())?
    var deviceType = ""
    
    override func viewWillAppear(_ animated: Bool) {
        sendDeviceListRequest()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
                
        initUI()
    }
    lazy var tableView: UITableView = {
        let vi = UITableView.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(10), width: SCREEN_WIDHT, height: SCREEN_HEIGHT-kFitWidth(10)-getNavigationBarHeight()-kFitWidth(100) - getBottomSafeAreaHeight()), style: .plain)
        vi.delegate = self
        vi.dataSource = self
        vi.rowHeight = UITableView.automaticDimension
        vi.register(DeviceSNTableViewCell.classForCoder(), forCellReuseIdentifier: "DeviceSNTableViewCell")
        vi.separatorStyle = .none
        vi.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        return vi
    }()
    lazy var emptyView : EmptyViewWithBtn = {
        let emptyViewT = EmptyViewWithBtn.init(frame: CGRect.zero)
        emptyViewT.isHidden = true
        emptyViewT.contentLabel.text = "还没有记录~"
        
        return emptyViewT
    }()
    lazy var addBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: SCREEN_HEIGHT - kFitWidth(100) - getBottomSafeAreaHeight(), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("新增音响", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(addAction), for: .touchUpInside)
        
        return btn
    }()
}

extension DeviceVoiceListVC{
    @objc func addAction() {
        let vc = BindSNVC()
        vc.bindType = self.deviceType
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension DeviceVoiceListVC{
    func initUI() {
        initNavi(titleStr: "音响列表")
        view.backgroundColor = WHColor_16(colorStr: "F8F8F8")
        
        view.addSubview(tableView)
        tableView.addSubview(emptyView)
        emptyView.center = CGPoint.init(x: SCREEN_WIDHT*0.5, y: self.tableView.frame.height*0.5)
        view.addSubview(addBtn)
    }
}

extension DeviceVoiceListVC:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        emptyView.isHidden = dataArray.count > 0 ? true : false
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DeviceSNTableViewCell") as! DeviceSNTableViewCell
        
        let dict = self.dataArray[indexPath.row]as? NSDictionary ?? [:]
        cell.updateUIForVoice(dict: dict)
        
        cell.updateBlock = {()in
            let vc = DeviceSNUpdateNameVC()
            vc.oldName = dict["posName"]as? String ?? ""
            vc.deviceId = dict["id"]as? String ?? ""
            self.navigationController?.pushViewController(vc, animated: true)
        }
        cell.showBlock = {()in
            let vc = MyQrCodeVC()
            vc.imageUrl = dict["imageUrl"]as? String ?? ""
            self.navigationController?.pushViewController(vc, animated: true)
        }
        cell.showSignCodeBlock = {()in
            let vc = MySignQrCodeVC()
            vc.imageUrl = dict["activityInfoUrl"]as? String ?? ""
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let dict = self.dataArray[indexPath.row]as? NSDictionary ?? [:]
        var rowHeight = kFitWidth(157)
//        if dict["posModel"]as? String ?? "" == "N7HD" && (dict["orderNo"]as? String ?? "").count > 0 && dict["orderNo"]as? String ?? "" != "null" && dict["orderNo"]as? String ?? "" != "<null>"{
        if dict["posModel"]as? String ?? "" == "N7HD"{
            rowHeight = rowHeight + kFitWidth(26)
            if (dict["orderNo"]as? String ?? "").count > 0 && dict["orderNo"]as? String ?? "" != "null" && dict["orderNo"]as? String ?? "" != "<null>"{
                rowHeight = rowHeight + kFitWidth(26)
                if (dict["deviceRemark"]as? String ?? "").count > 0 {
                    rowHeight = rowHeight + kFitWidth(26)
                }
                if (dict["errorCode"]as? String ?? "").count > 0 {
                    rowHeight = rowHeight + kFitWidth(26)
                }
            }
        }
        
        return rowHeight// dict["izRelation"]as? String ?? "" == "Y" ? kFitWidth(174) : kFitWidth(157)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dict = self.dataArray[indexPath.row]as? NSDictionary ?? [:]
        if self.choiceBlock != nil{
            self.choiceBlock!(dict)
        }
        self.backTapAction()
    }
}

extension DeviceVoiceListVC{
    func sendDeviceListRequest() {
        let param = ["type":"VOICE_BOX"]
        WHNetworkUtil.shareManager().POST(urlString: URL_Device_list, parameters: param as [String : AnyObject],isNeedToast: true,vc: self) { responseObject in
            DLLog(message: "\(responseObject)")
            
            self.dataArray = responseObject["data"]as? NSArray ?? []
            self.tableView.reloadData()
        }
        
    }
}
